//
//  HJZStatus.m
//  HJZWeiBo
//
//  Created by 蒋晨成 on 16/6/1.
//  Copyright © 2016年 蒋晨成. All rights reserved.
//

#import "HJZStatus.h"
#import "NSDate+LCExtend.h"


@implementation HJZStatus

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    if ([key isEqualToString:@"pic_ids"]) {
        NSMutableArray *picurls = [NSMutableArray array];
        for (NSString *picid in value) {
            NSString *picurl = [NSString stringWithFormat:@"http://ww4.sinaimg.cn/thumbnail/%@.jpg",picid];
            NSDictionary *picDic = @{@"thumbnail_pic":picurl};
            [picurls addObject:picDic];
        }
        self.pic_urls = picurls;
    }
}

- (void)setValue:(id)value forKey:(NSString *)key{
    if ([key isEqualToString:@"user"]) {
        self.user = [[HJZUser alloc] init];
        [self.user setValuesForKeysWithDictionary:value];
        
    }else if ([key isEqualToString:@"retweeted_status"]) {
        
        self.retweeted_status = [[HJZStatus alloc] init];
        [self.retweeted_status setValuesForKeysWithDictionary:value];
        
    }else {
        
        [super setValue:value forKey:key];
        
    }
}


/**
 *  修正微博的发送时间
 */
- (NSString *)created_at
{
    // 获取微博发送时间 Fri May 09 16:30:34 +0800 2014
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [formatter setLocale:locale];
    
    formatter.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
    NSDate *createdDate = [formatter dateFromString:_created_at];
    
    // 判断
    if (createdDate.isToday) { // 今天以内
        if (createdDate.deltaToNow.hour >= 1) { // 1个小时以前
            return [NSString stringWithFormat:@"%ld小时前", createdDate.deltaToNow.hour];
        } else if (createdDate.deltaToNow.minute >= 1) { // 1分钟以前
            return [NSString stringWithFormat:@"%ld分钟前", createdDate.deltaToNow.minute];
        } else { // 1分钟以内
            return @"刚刚";
        }
    } else if (createdDate.isYesterday) { // 昨天以前
        formatter.dateFormat = @"昨天 HH:mm";
        return [formatter stringFromDate:createdDate];
    } else if (createdDate.isThisYear) { // 今年以内
        formatter.dateFormat = @"MM-dd HH:mm";
        return [formatter stringFromDate:createdDate];
    } else { // 去年以前
        formatter.dateFormat = @"yyyy-MM-dd HH:mm";
        return [formatter stringFromDate:createdDate];
    }
}

/**
 *  修正微博的来源
 */
- (void)setSource:(NSString *)source
{
    if (source.length) {
        NSInteger location = [source rangeOfString:@">"].location + 1;
        NSInteger length = [source rangeOfString:@"</"].location - location;
        
        _source = [NSString stringWithFormat:@"来自%@", [source substringWithRange:NSMakeRange(location, length)]];
    } else {
        _source = @"未知来源";
    }
}




@end
